package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.item.entity.Sku;
import com.leyou.item.mapper.SkuMapper;
import com.leyou.item.service.SkuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;


@Service
public class SkuServiceImpl extends ServiceImpl<SkuMapper,Sku> implements SkuService {
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void minusStock(Map<Long, Integer> cartsMap) {


        String minusStatement = "com.leyou.item.mapper.SkuMapper.minusStockAddSold";

        //mybatis-plus批量执行器
        executeBatch(sqlSession -> {

            cartsMap.entrySet().forEach(entry -> {
                Long skuId = entry.getKey();
                Integer num = entry.getValue();
                //map形式的参数
                Map<String, Object> paramMap = new HashMap<>();
                paramMap.put("skuId", skuId);
                paramMap.put("num", num);
                //预处理添加，把所有要执行的update，添加进来
                sqlSession.update(minusStatement, paramMap);
            });

            //当添加完成后批量统一执行
            sqlSession.flushStatements();

        });


    }
}
